package com.szgr.baoyin.activity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.GridView;
import android.widget.TextView;

import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.CalendarGridAdapter;
import com.szgr.baoyin.model.Day;
import com.szgr.baoyin.myview.NoScrollGridVIew;
import com.szgr.baoyin.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class CalendarActivity extends Activity implements View.OnClickListener {
    private List<Day> data;
    private CalendarGridAdapter gridAdapter;
    private int mm = 1;
    private TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        data = new ArrayList<>();
        NoScrollGridVIew calendarView = (NoScrollGridVIew) findViewById(R.id.calendar_grid_view);
        gridAdapter = new CalendarGridAdapter(this, data);
        calendarView.setAdapter(gridAdapter);
        initData("2015-01");

        
        TextView tv_down = (TextView) findViewById(R.id.calendar_down);
        tv_title = (TextView) findViewById(R.id.action_bar_title);
        tv_down.setOnClickListener(this);
    }

    private void initData(String month) {
        data.clear();
        int days = DateUtils.getMonthDays(month);
        int dayOfWeek = DateUtils.getDayOfWeek(month);
        int d = 1;
        for(int i = 1;i <= 42; i++){
            Day day = new Day();
            if(i >= dayOfWeek && i < days + dayOfWeek){

                String temp  = d + "";
                if( d < 10){
                    temp = "0" + d;
                }
                day.setNum(temp);
                day.setDate(month + "-" + temp);
                d ++ ;
            }
            data.add(day);
        }
        gridAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.calendar_down:
                String str = "";
                mm ++ ;
                if(mm < 10){
                    str = "0" + mm;
                }else {
                    str = "" + mm;
                }

                initData("2015-" + str);
                tv_title.setText("2015-" + str);
                break;
        }
    }
}
